$(document).ready(function() {
	var lang = $("#display-lang").val();
	var ds_id = $("#dataset").val();
	showDocuments(0, 50, lang, true, ds_id);
});

function showDocuments(start, length, lang, newFlag, ds_id) {
	var data = {};
	data["start"] = start;
	data["length"] = length;
	data["lang"] = lang;
	data["ds_id"] = ds_id;
	data = JSON.stringify(data);
	data = ("document=" + encodeURIComponent(data));
	$
			.ajax({
				url : "/sview/datasetDocs",
				async : true,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					var array = result["result"];
					var flag = false;
					if (newFlag) {
						$(".entity-list").html("");
					}
					if ($(".entity-list ol").length > 0)
						flag = true;
					if (!flag) {
						var olt = $("<ol></ol>");
						$(".entity-list").append(olt);
					}
					var ol = $(".entity-list>ol");
					for ( var i = 0; i < array.length; i++) {
						var li = $("<li></li>");
						ol.append(li);
						var element = array[i];
						li.addClass("lentity");
						var id = element["id"];
						var div = $('<div class="sc split"></div>');
						li.append(div);
						var a = $("<a></a>");
						var href = "/sview/dataset/u";
						href = href + id;
						a.attr("href", href);
						div.append(a);
						var spanName = $("<span></span>");
						var name = element["name"];
						spanName.text(name);
						spanName.addClass("entity");
						a.append(spanName);
					}
					$(".entity-list ol li:last>div").addClass("final");
					$(".more-container").remove();
					if (array.length > 0) {
						var div = $("<div class='more-container'></div>");
						var moreDiv = $("<div class='moreDiv'></div>");
						var a = null;
						if (lang == 'en'){
							a = $('<a href="javascript:void(0)"><span class="tip">Load More</span><span class="arrowDown"></span></a>');
						}else if(lang == 'zh'){
							a = $('<a href="javascript:void(0)"><span class="tip">更多</span><span class="arrowDown"></span></a>');
						}
						
						a.bind("click", function() {
							showDocuments(start + length, length, lang, false,
									ds_id);
						});
						moreDiv.append(a);
						div.append(moreDiv);
						$(".entity-list").parent().append(div);
					}
				}
			});
}